package com.dhh.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

import com.dhh.R;
import com.dhh.dao.DataBaseHelper;
import com.dhh.util.Constant;
import com.dhh.util.manager.WeightManager;
import com.dhh.vo.Weight;

import greendroid.app.GDActivity;

public class WeightAddActiity extends GDActivity implements View.OnClickListener {	
	private DataBaseHelper db;
	private WeightManager wManager;
	private EditText weightText;
	private Button addButton;
	private DatePicker datePicker;
	
	@Override  
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setActionBarContentView(R.layout.weight_add);
		
        db = new DataBaseHelper(getBaseContext());
        db.openDataBase();        
        wManager = new WeightManager(db);
        
		initActionBar();
		initView();
	}
	
	private void initActionBar(){
		getActionBar().setTitle("My Weight");
        getActionBar().setBackgroundColor(Color.rgb(14, 166, 249));
        getActionBar().getChildAt(0).
        	setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(WeightAddActiity.this,HomeActivity.class);	
					startActivity(intent);
				}}); 
	}
	
	private void initView(){
		weightText = (EditText) this.findViewById(R.id.wd_weight);
		addButton = (Button) this.findViewById(R.id.wd_add);
		datePicker = (DatePicker) this.findViewById(R.id.wd_date);
		
		addButton.setOnClickListener(this);
	}
	
	@Override
	public void onStart() {
        super.onStart();     
	}
	
	@Override
    public void onResume(){
    	super.onResume();
        db.openDataBase();       
    }
    
    @Override
	protected void onPause(){
		super.onPause();
		if (db != null) {
			db.close();
		}
	}
    
    @Override
	protected void onStop(){
		super.onStop();
	}

	@Override
	public void onClick(View v) {
		StringBuffer sb = new StringBuffer();
		sb.append(datePicker.getYear());
		sb.append("-");
		sb.append(datePicker.getMonth()+1);
		sb.append("-");
		sb.append(datePicker.getDayOfMonth());
		String date = sb.toString();
		
		if(isExist(date)){
			Toast.makeText(getBaseContext(),"The log of "+date+" has been exist!", Toast.LENGTH_LONG).show();
		}else if(R.id.wd_add==v.getId()){
			Weight w = new Weight();
			
			
			w.setDate(date);
			w.setWeight(weightText.getText().toString());
			wManager.addWeight(w);
			
			Intent intent = null;
			if(getIntent().getExtras().getString(Constant.FROM_ACTIVITY).equals(Constant.FA_WEIGHT_LOG)){
				intent = new Intent(WeightAddActiity.this,WeightLogActivity.class);
			}else{
				intent = new Intent(WeightAddActiity.this,HomeActivity.class);
			}
			startActivity(intent);
		}		
	}
	
	private boolean isExist(String date){
		Weight w = wManager.getWeightLog(date);
		if(w.getId()!=null){
			return true;
		}
		
		return false;
	}
}
